import 'package:flutter_admin/models/user.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_dialog.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/product_productunitprice_odoo_api.dart';
import 'package:flutter_admin/api/odoo/product_template_odoo_api.dart';
import 'package:flutter_admin/constants/enum.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/product_productunitprice.dart';
import 'package:flutter_admin/models/odoo/product_template.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_dialog.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_productunitprice_edit.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_productunitprice_dialog.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';
import 'package:flutter_admin/utils/utils.dart';

class UserList extends StatefulWidget {
  UserList({Key? key, this.workshopId}) : super(key: key);

  String? workshopId;

  @override
  _UserListState createState() => _UserListState();
}

class _UserListState extends State<UserList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page =
      PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  UserInfo userInfo = UserInfo();
  List<UserInfo> selectedList = [];
  bool flag = true;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      userInfo.deptId = widget.workshopId;
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            width: 280,
            label: '名称',
            value: userInfo.name,
            onSaved: (v) {
              userInfo.name = v;
            },
          ),
        ],
      ),
    );

    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      // actions: !isDisplayDesktop(context)
      //     ? [
      //         _operationList(),
      //       ]
      //     : [],
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        if (flag) {
          Navigator.pop(context, selected);
        }
        flag = false;
      },
      columns: [
        DataColumn(
          label: Text('用户名称'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
        DataColumn(
          label: Text('职位'),
          //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
        ),
      ],
      getCells: (m) {
        UserInfo userInfo = UserInfo.fromMapTow(m);
        return [
          //isDisplayDesktop(context)?DataCell(
          //  CryButtonBar(
          //    children: [
          //      CryButton(
          //          iconData: Icons.edit,
          //          tip: S.of(context).modify,
          //          onPressed: () => _edit(productProductunitprice)),
          //    ],
          //  ),
          // ):DataCell(Container()),
          DataCell(Text(userInfo.name ?? '--')),
          DataCell(Text(userInfo.deptName ?? '--')),
          // DataCell(Text(productTemplate.description ?? '--')),
          // DataCell(Text(productTemplate.categName ?? '--')),
        ];
      },
    );
    // _getSelectList();
    CryButtonBar buttonBar = CryButtonBar(
      children: <Widget>[
        CryButtons.query(context, () => _query()),
        // CryButtons.reset(context, () => _reset()),
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text('选择人员'),
      ),
      key: scaffoldStateKey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          isDisplayDesktop(context) ? form : Container(),
          isDisplayDesktop(context) ? buttonBar : Container(),
          Expanded(
              child: SingleChildScrollView(
                  child: Container(
            width: 650,
            child: table,
          ))),
        ],
      ),
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
  }

  // _reset() {
  //   userInfo = UserInfo();
  //   formKey.currentState!.reset();
  //   _loadData();
  // }

  _query() {
    formKey.currentState?.save();
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi = await ProductTemplateOdooApi.userPage(
        data: {"page": page, "domainList": userInfo.toDomainList()});
    if (responseBodyApi.success!) {
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
      // _getSelectList();
    }
  }

  _search() {
    return showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: ProductDialog(),
      ),
    ).then((v) {
      if (v != null) {
        userInfo = v;
        _query();
      }
    });
  }

  // _getSelectList() {
  //   selectedList = tableKey.currentState
  //           ?.getSelectedList(page)
  //           .map<UserInfo>((e) => UserInfo.fromMapTow(e))
  //           .toList() ??
  //       [];
  //   setState(() {});
  // }

  // _operationList() {
  //   return PopupMenuButton(
  //     onSelected: (dynamic v) {
  //       if (v == OperationType.query) {
  //         _search();
  //       }
  //     },
  //     itemBuilder: (context) => <PopupMenuEntry<OperationType>>[
  //       PopupMenuItem<OperationType>(
  //         value: OperationType.query,
  //         child: ListTile(
  //           leading: const Icon(Icons.search),
  //           title: Text('查询'),
  //         ),
  //       ),
  //     ],
  //   );
  // }
}
